
package com.meteor.raft.executor;

import java.util.concurrent.*;

public class GlobalExecutor {

    public static final long HEARTBEAT_INTERVAL_MS = TimeUnit.SECONDS.toMillis(1L);

    public static final long LEADER_TIMEOUT_MS = TimeUnit.SECONDS.toMillis(10L);

    public static final long RANDOM_MS = TimeUnit.SECONDS.toMillis(5L);

    public static final long TICK_PERIOD_MS = TimeUnit.MILLISECONDS.toMillis(500L);

    public static void main(String[] args) {
        System.out.print(HEARTBEAT_INTERVAL_MS);
    }

    private static ScheduledExecutorService executorService =
        new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);

                t.setDaemon(true);
                t.setName("com.raft.timer");

                return t;
            }
        });

    public static void registerMasterElection(Runnable runnable) {
        executorService.scheduleAtFixedRate(runnable, 0, HEARTBEAT_INTERVAL_MS, TimeUnit.MILLISECONDS);
    }

    public static void registerHeartbeat(Runnable runnable) {
        executorService.scheduleWithFixedDelay(runnable, 0, HEARTBEAT_INTERVAL_MS, TimeUnit.MILLISECONDS);
    }


    public static void registeIsReady(Runnable runnable) {
        executorService.scheduleWithFixedDelay(runnable, 0, HEARTBEAT_INTERVAL_MS, TimeUnit.MILLISECONDS);
    }

}
